package com.fortune.controller;

import com.faneui.common.bean.BaseResp;
import com.faneui.common.bean.IdReq;
import com.faneui.common.bean.OptionVo;
import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.ListUtils;
import com.faneui.common.utils.MapUtils;
import com.fortune.dal.dao.CustCertInfoDAO;
import com.fortune.dal.dao.CustDAO;
import com.fortune.dal.model.Cust;
import com.fortune.dal.model.CustCertInfo;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("custcert")
public class CustCertController {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private CustCertInfoDAO custCertInfoDAO;

    @Autowired
    private CustDAO custDAO;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView indexOfAll() throws BusinessException {
        ModelAndView mv = new ModelAndView("custcert");
        mv.addObject("menuFlag", "custcert");
        return mv;
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public List<? extends Object> list(@ModelAttribute CustCertInfo custCertInfo) throws BusinessException {
        List<Map<String, Object>> list = Lists.newArrayList();
        try {
            List<CustCertInfo> lists = custCertInfoDAO.queryByModel(custCertInfo);
            if (ListUtils.isNotEmpty(lists)) {
                for (CustCertInfo custCert : lists) {

                    Map<String, Object> map = MapUtils.parseMap(custCert);
                    map.put("id",custCert.getId());
                    Integer custId = custCert.getCustId();

                    Cust cust = custDAO.queryById(custId);
                    String custName = cust.getName();
                    map.put("custName",custName);


                    list.add(map);
                }

            }
        } catch (Exception ex) {
            logger.error("",ex);
        }
        if(ListUtils.isEmpty(list)) {
            list = Lists.newArrayList() ;
        }

        return list;
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public BaseResp saveProject(@RequestBody CustCertInfo custCertInfo) {
        BaseResp resp = new BaseResp();
        try {
            if(custCertInfo.getCustId() == null) {
                throw new BusinessException("请选择客户") ;
            }
            if(StringUtils.isBlank(custCertInfo.getCertType())) {
                throw new BusinessException("请选择证件类型") ;
            }
            if(custCertInfo.getId() == null) {



                CustCertInfo param = new CustCertInfo();
                param.setCustId(custCertInfo.getCustId());
                param.setCertType(custCertInfo.getCertType());
                if(ListUtils.isNotEmpty(custCertInfoDAO.queryByModel(param))) {
                    throw new BusinessException("证件重复") ;
                }
                custCertInfoDAO.insert(custCertInfo);
            } else {
                custCertInfoDAO.updateById(custCertInfo) ;
            }
        } catch (Exception ex) {
            logger.error("", ex);
            resp.setSuccess(false);
            resp.setMsg(ex.getMessage());
        }
        return resp;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public BaseResp deleteProject(@RequestBody IdReq idReq) {
        BaseResp resp = new BaseResp();
        try {
            custCertInfoDAO.deleteById(Integer.valueOf(idReq.getId())) ;
        } catch (Exception ex) {
            logger.error("", ex);
            resp.setSuccess(false);
            resp.setMsg(ex.getMessage());
        }
        return resp;
    }

    @RequestMapping(value = "/select/{queryType}", method = RequestMethod.GET)
    @ResponseBody
    public List<OptionVo> selectOptionVos(@PathVariable String queryType) {
        List<OptionVo> optionVos = Lists.newArrayList();
        try {
            if ("cust".equals(queryType)) {
                List<Cust> custs = custDAO.queryByModel(new Cust()) ;
                for (Cust vo : custs) {
                    optionVos.add(new OptionVo( vo.getName(),vo.getId()+""));
                }
            } else if ("certtype".equals(queryType)) {
                optionVos.add(new OptionVo("身份证","1"));
                optionVos.add(new OptionVo("房产证","2" ));
                optionVos.add(new OptionVo("驾驶证","3"));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return optionVos;
    }
}
